import Vue from "vue";
import App from "./App";
Vue.prototype.$bus = new Vue();//创造一个公共实例，每个vue实例都具备这个$on $emit $off
Vue.prototype.$dispatch = function(eventName,componentName,value){
  let parent = this.$parent;
  while(parent){
      //触发指定组件上的事件，而不是全部向上找
      if(parent.$options.name === componentName){
          parent.$emit(eventName,value); // 没有绑定触发，不会有任何影响
          return
      }
      parent = parent.$parent;
  }
}
Vue.prototype.$broadcast = function(eventName,componentName,value){
  //需要找到所有子组件进行触发
  let children = this.$children;
  let arr = []
  function broadcast(children){
      for(let i=0;i<children.length;i++){
          let child = children[i];
          if(componentName === child.$options.name){
            if(eventName){
              child.$emit(eventName,value);
            }else{
              arr.push(child)
            }
          }
          if(child.$children){
            broadcast(child.$children)
          }
      }
  }
  broadcast(children)
  return arr
}
let vm = new Vue({
    el:'#app',
    render:h=>h(App)
});